/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.action;

import java.awt.event.KeyEvent;

import com.bluestone.BaseAction;
import com.bluestone.context.IContext;
import com.bluestone.scripts.ActionScript;
import com.bluestone.scripts.Attributes;
import com.bluestone.scripts.Browser;
import com.bluestone.scripts.Element;
import com.bluestone.scripts.Page;
import com.bluestone.util.Util;

/**
 * Move to an element in the page and do some actions.
 * If the element is the type of the Combox ,List or Radio, the value is the index of the values in the Combox,List or Radio.
 * @author <a href="mailto:bluestone.daniel.q@gmail.com">daniel.q</a>
 */
public class FocusAction extends BaseAction {


	public FocusAction(ActionScript action) {
		super(action);
		// TODO Auto-generated constructor stub
	}

	public boolean execute(IContext context) {
		if (!super.execute(context)) {
			return false;
		}
		try {
			Element element = null;
			Page page = null;
			String value = action.getPara("value");
			String target = action.getPara("target");
			// check target , return false if null
			if (target == null || target.length() == 0)
				return false;

			// get element and page from module file
			element = this.actionContext.getProjectContext().getProject().searchElement(target);
			if (element == null) {
				Util.error(Util.ERROR1 + target);
				return false;
			}

			// return false if element does not define the focus index value
			if (element.getIndex() < 0)
				return false;

			page = (Page) element.getParent();
			if (page == null) {
				Util.error(Util.ERROR1
						+ target.substring(0, target.lastIndexOf("|")));
				return false;
			}

			String indexPos = "";
			String topblankpos = this.actionContext.getProjectContext().getProject().getBrowser()
					.getTopBlankPos();
			String x = topblankpos.split(",")[0];
			String y = topblankpos.split(",")[1];

			if ("TRUE".equalsIgnoreCase(page.getBrowserBar())) {
				indexPos = x
						+ ","
						+ (this.actionContext.getProjectContext().getProject().getBarHeight() + Integer
								.parseInt(y));
			} else {
				indexPos = x
						+ ","
						+ (this.actionContext.getProjectContext().getProject().getTitlebarHeight() + Integer
								.parseInt(y));
			}
			int count = element.getIndex();
			String offset = action.getPara("offset");
			if (offset != null && offset.length() > 0) {
				count = count + Integer.parseInt(offset);
			}

			if (count <= 0) {
				return true;
			}

			// move to start focus position in web page
			ActionScript a = new ActionScript();
			a.setType(ActionFactory.MOVEACTION);
			a.setPara("target", Attributes.SCREEN);
			a.setPara("value", indexPos);
			ActionFactory.createAction(a).execute(context);
			
			ActionScript b = new ActionScript();
			b.setType(ActionFactory.CLICKACTION);
			ActionFactory.createAction(b).execute(context);
			
			robot.delay(Util.getDelayTime(Util.DELAY200));
			
			ActionScript c = new ActionScript();
			c.setType(ActionFactory.INPUTKEYACTION);
			c.setPara("target", InputKeyAction.HOMEKEY);
			ActionFactory.createAction(c).execute(context);
			
			robot.delay(Util.getDelayTime(Util.DELAY200));
			
			ActionScript d = new ActionScript();
			d.setType(ActionFactory.MOVEACTION);
			d.setPara("target", Attributes.SCREEN);
			d.setPara("value", indexPos);
			
			ActionFactory.createAction(d).execute(context);
			
			ActionScript e = new ActionScript();
			e.setType(ActionFactory.CLICKACTION);
			ActionFactory.createAction(e).execute(context);

			robot.delay(Util.getDelayTime(Util.DELAY200));

			for (int i = 0; i < count; i++) {
				robot.keyPress(KeyEvent.VK_TAB);
				robot.delay(Util.getDelayTime(Util.DELAY200));
				robot.keyRelease(KeyEvent.VK_TAB);
				robot.delay(Util.getDelayTime(Util.DELAY300));
			}

			robot.delay(Util.getDelayTime(Util.DELAY500));
			String type = element.getType();
			String btype = this.actionContext.getProjectContext().getProject().getBrowser().getType();
			if (Attributes.TYPE_BUTTON.equalsIgnoreCase(type)
					|| Attributes.TYPE_CHECKBOX.equalsIgnoreCase(type)) {
				ActionScript f = new ActionScript();
				f.setType(ActionFactory.INPUTKEYACTION);
				f.setPara("target", InputKeyAction.SPACEKEY);
				ActionFactory.createAction(f).execute(context);
			} else if (Attributes.TYPE_LIST.equalsIgnoreCase(type)) {

				if (Browser.FIREFOX2.equalsIgnoreCase(btype)
						|| Browser.FIREFOX3.equalsIgnoreCase(btype)) {
					ActionScript g = new ActionScript();
					g.setType(ActionFactory.INPUTKEYACTION);
					g.setPara("target", InputKeyAction.DOWNKEY);
					ActionFactory.createAction(g).execute(context);
				} else {
					ActionScript h = new ActionScript();
					h.setType(ActionFactory.INPUTKEYACTION);
					h.setPara("target", InputKeyAction.SPACEKEY);
					ActionFactory.createAction(h)
							.execute(context);
				}

				if (value != null && value.length() > 0) {
					int pos = Integer.parseInt(value);
					for (int i = 0; i < pos - 1; i++) {
						ActionScript m = new ActionScript();
						m.setType(ActionFactory.INPUTKEYACTION);
						m.setPara("target", InputKeyAction.DOWNKEY);
						m.setPara("delay", ""+Util.DELAY200);
						ActionFactory.createAction(m)
								.execute(context);
					}
				}
			} else if (Attributes.TYPE_LINK.equalsIgnoreCase(type)
					|| Attributes.TYPE_TAB.equalsIgnoreCase(type)) {
				ActionScript n = new ActionScript();
				n.setType(ActionFactory.INPUTKEYACTION);
				n.setPara("target", InputKeyAction.ENTERKEY);
				ActionFactory.createAction(n).execute(context);
			} else if (Attributes.TYPE_COMBOX.equalsIgnoreCase(type)) {
				if (value != null && value.length() > 0) {
					int pos = Integer.parseInt(value);
					for (int i = 0; i < pos - 1; i++) {
						ActionScript o = new ActionScript();
						o.setType(ActionFactory.INPUTKEYACTION);
						o.setPara("target", InputKeyAction.DOWNKEY);
						o.setPara("delay", ""+Util.DELAY200);
						ActionFactory.createAction(o)
								.execute(context);
					}
					if (Browser.FIREFOX2.equalsIgnoreCase(btype)
							|| Browser.FIREFOX3.equalsIgnoreCase(btype)) {
						ActionScript p = new ActionScript();
						p.setType(ActionFactory.INPUTKEYACTION);
						p.setPara("target", InputKeyAction.ENTERKEY);
						
						ActionFactory.createAction(p)
								.execute(context);
					}

				}
			} else if (Attributes.TYPE_RADIO.equalsIgnoreCase(type)) {
				if (value != null && value.length() > 0) {
					int pos = Integer.parseInt(value);
					for (int i = 0; i < pos - 1; i++) {
						ActionScript q = new ActionScript();
						q.setType(ActionFactory.INPUTKEYACTION);
						q.setPara("target", InputKeyAction.DOWNKEY);
						q.setPara("delay", ""+Util.DELAY200);
						ActionFactory.createAction(q)
								.execute(context);
					}
				}

			} else if (Attributes.TYPE_TEXTFIELD.equalsIgnoreCase(type)) {
				if (value != null && value.length() > 0) {
					ActionScript r = new ActionScript();
					r.setType(ActionFactory.INPUTTEXTACTION);
					r.setPara("value", value);
					ActionFactory.createAction(r).execute(context);
				}
			}
			String delay = action.getPara("delay");
			if (delay != null && delay.length() > 0) {
				robot.delay(Util.getDelayTime(Integer.parseInt(delay)));
			} else {
				robot.delay(Util.getDelayTime(Util.FOCUSDELAY));
			}

		} catch (Exception me) {
			me.printStackTrace();
			Util.error(me);
			return false;
		}
		return true;
	}

}
